package com.gxa.mall.controller;

import com.gxa.mall.common.CommonResult;
import com.gxa.mall.common.SendMail;
import com.gxa.mall.model.Admin;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 邮件发送控制层
 */
@ResponseBody
@Controller
@RequestMapping(path = "/sendemail")
public class SendMailController {
    @RequestMapping(path = "send")
    public CommonResult send(Admin admin, HttpServletRequest request, HttpServletResponse response) {
        CommonResult commonResult = null;
        try {
            // 获取用户的邮箱
            // 实例化用户对象
            // 实例化一个发送邮件的对象
            SendMail mySendMail = new SendMail();
            // 如果查到该用户，并且用户名和页面输入相同验证成功，发送邮件

            // 生成六位验证码
            int code = (int) ((Math.random() * 9 + 1) * 100000);

            //发送邮件
            mySendMail.sendMail(admin.getEmail(), "医药管理管理系统提醒，您正在注册成为本公司的用户,您的验证码为:" + code);

            commonResult = new CommonResult(200, "发送成功", code);
            return commonResult;
        }catch (Exception e){
            System.out.println("程序异常");
            e.printStackTrace();
        }
        return commonResult;
    }
}

